<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>台账列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!-- 搜索表单 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">操作类型</label>
                            <div class="layui-input-inline">
                                <select name="operationType" >
                                    <option value="">请选择</option>
                                    <option value="1">入库</option>
                                    <option value="2">出库</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">操作时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startTime" id="startTime" placeholder="开始时间" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline">
                                <input type="text" name="endTime" id="endTime" placeholder="结束时间" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">计划类型</label>
                            <div class="layui-input-inline">
                                <select name="planType" id="planTypeSelect" >
                                    <option value="">全部</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="resetSearchBtn">
                                <i class="layui-icon">ဂ</i> 重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!-- 表格容器 -->
        <table class="layui-hide" id="ledgerTable" lay-filter="ledgerTableFilter"></table>
    </div>
</div>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;

        // 初始化日期控件
        // 初始化日期控件
        laydate.render({
            elem: '#startTime',
            theme: '#47A5EE',
            format: 'yyyy-MM-dd HH:mm:ss', // 设置完整格式
            type: 'datetime'
        });

        laydate.render({
            elem: '#endTime',
            theme: '#47A5EE',
            format: 'yyyy-MM-dd HH:mm:ss',
            type: 'datetime'
        });

        // 初始化表格
        table.render({
            elem: '#ledgerTable',
            url: '/ledger/queryPage',
            method: 'post',
            contentType: 'application/json',
            cols: [[
                {field: 'operationType', title: '操作类型', width: 100, templet: function(d) {
                        return d.operationType === '1' ? '入库' : '出库';
                    }},
                {field: 'operationTime', title: '操作时间', width: 150},
                {field: 'planType', title: '计划类型', width: 120},
                {field: 'projectNumber', title: '项目编号', width: 130},
                {field: 'projectName', title: '项目名称', width: 150},
                {field: 'materialsDescription', title: '物料描述', minWidth: 150},
                {field: 'quantity', title: '数量', width: 100},
                {field: 'propertyNumber', title: '资产编号', width: 130},
                {field: 'materialsCode', title: '物料编码', width: 130},
                {field: 'remark', title: '备注', minWidth: 150}
            ]],
            limits: [10, 20, 50],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function(res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            response: {
                statusName: 'code',
                statusCode: 0,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            }
        });

        // 加载计划类型下拉数据
        function loadPlanTypes() {
            $.getJSON('/ledger/getPlan', function(res) {
                if (res.code === 0 && res.data && res.data.length > 0) {
                    var options = '';
                    res.data.forEach(function(item) {
                        options += '<option value="' + item.name + '">' + item.name + '</option>';
                    });
                    $("#planTypeSelect").html('<option value="">请选择计划类型</option>' + options);
                    form.render('select'); // 重新渲染 select
                }
            });
        }
        // 页面初始化加载计划类型
        loadPlanTypes();
        // 搜索提交事件
        form.on('submit(data-search-btn)', function (data) {
            var formData = data.field;
            // 构建参数对象，排除空值字段
            var params = {};
            if (formData.operationType !== '') {
                params.operationType = formData.operationType;
            }
            if (formData.planType !== '') {
                params.planType = formData.planType;
            }
            if (formData.startTime !== '') {
                params.startTime = formData.startTime;
            }
            if (formData.endTime !== '') {
                params.endTime = formData.endTime;
            }

            table.reload('ledgerTable', {
                page: { curr: 1 },
                where: params
            });

            return false;
        });


        // 重置按钮点击事件
        $('#resetSearchBtn').on('click', function () {
            form.val("layui-form", {
                operationType: "",
                planType: "",
                startTime: "",
                endTime: ""
            });
            table.reload('ledgerTable', {
                page: {curr: 1},
                where: {}
            });
        });
    });
</script>
</body>
</html>
